package com.kaibes.orm.spring.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.kaibes.orm.core.core.TableService;
import com.kaibes.orm.core.listener.DeleteListener;
import com.kaibes.orm.curd.Delete;

@SuppressWarnings("rawtypes")
public abstract class DeleteListenerWithData<S extends TableService> implements DeleteListener {

    @Autowired
    protected S service;
    protected ThreadLocal<List<?>> store = new ThreadLocal<>();
    
    @SuppressWarnings("unchecked")
    @Override
    public final void beforeDelete(Delete delete) throws Exception {
        List<?> entityList = service.list(delete.getQueries());
        if (!entityList.isEmpty()) {
            store.set(entityList);
        }
    }
    
    @Override
    public final void afterDelete(Delete delete, int count) throws Exception {
        List<?> entityList = store.get();
        if (entityList != null) {
            store.remove();
            afterDelete(entityList);
        }
    }
    
    protected abstract void afterDelete(List<?> entityList) throws Exception;
}
